package com.example.cognitivesystemserver.controller;

import com.example.cognitivesystemserver.Exceptions.ParamsException;
import com.example.cognitivesystemserver.base.ResultInfo;
import com.example.cognitivesystemserver.base.ResultType;
import com.example.cognitivesystemserver.pojo.Score;
import com.example.cognitivesystemserver.pojo.Student;
import com.example.cognitivesystemserver.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    //添加学生信息
    @PostMapping("/student/addStudent")
    @ResponseBody
    public Object addStudent(Student student) {
        ResultInfo resultInfo=new ResultInfo();
        try {
            studentService.addStudent(student);
            resultInfo.setMsg("添加学生成功！");

        }catch (ParamsException p){
            resultInfo.setCode(p.getCode());
            resultInfo.setMsg(p.getMsg());
            p.printStackTrace();
        }catch (Exception e){
            resultInfo.setCode(500);
            resultInfo.setMsg("添加学生失败！");
            e.printStackTrace();
        }

        return resultInfo;
    }

    //删除学生信息
    @PostMapping("/student/deleteStudent")
    @ResponseBody
    public Object deleteStudent(String id) {
        ResultType resultType =new ResultType();
        try {
            System.out.println("删除学生"+id);
            studentService.deleteStudent(id);
            resultType.setMessage("删除学生成功！");
            System.out.println("删除学生id:"+id);
            resultType.setCode(200);

        }catch (ParamsException p){
            resultType.setCode(p.getCode());
            resultType.setMessage(p.getMsg());
            p.printStackTrace();
        }catch (Exception e){
            resultType.setCode(500);
            resultType.setMessage("删除学生失败！");
            e.printStackTrace();
        }

        return resultType;
    }

    //修改学生信息
    @PostMapping("/student/UpdateStudent")
    @ResponseBody
    public Object updateStudent(Student student) {
        ResultInfo resultInfo=new ResultInfo();
        try {
            studentService.updateStudent(student);
            resultInfo.setMsg("修改学生成功！");

        }catch (ParamsException p){
            resultInfo.setCode(p.getCode());
            resultInfo.setMsg(p.getMsg());
            p.printStackTrace();
        }catch (Exception e){
            resultInfo.setCode(500);
            resultInfo.setMsg("修改学生失败！");
            e.printStackTrace();
        }

        return resultInfo;
    }

    //查询某个学生信息
    @GetMapping("/student/getStudent")
    public Object getStudent(String StuId) {
        ModelAndView modelAndView=new ModelAndView();
        Student student= studentService.getStudent(StuId);
        modelAndView.addObject("student",student);
        modelAndView.setViewName("student");
        return student;
    }

    //查询全部学生信息
    @GetMapping("/student/getAllStudent")
    public Object getAllStudent() {
        List<Student> studentList=studentService.getAllStudent();
        return new ResultType(0,"",studentList);
    }

    //查询具体某个学生成绩
    @GetMapping("/student/getStuScore")
    public Object getStudentScore(String StuId) {
        List<Score> scores=studentService.getStudentScore(StuId);
        System.out.println("getStuScore"+" StuId:"+StuId);
        return new ResultType(0,"",scores);
    }

    //查询全部学生成绩
    @GetMapping("/student/getAllStuScore")
    public Object getAllStuScore() {
        List<Score> scoreList=studentService.getAllStuScore();
        return new ResultType(0, "", scoreList);
    }

    //删除学生成绩
    @PostMapping("/student/deleteStuScore")
    @ResponseBody
    public Object deleteStuScore(String stuId) {
        ResultType resultType =new ResultType();
        try {
            System.out.println(stuId+"删除成绩");
            studentService.deleteStuScore(stuId);
            resultType.setMessage("删除成功！");
            resultType.setCode(200);

        }catch (ParamsException p){
            resultType.setCode(p.getCode());
            resultType.setMessage(p.getMsg());
            p.printStackTrace();
        }catch (Exception e){
            resultType.setCode(500);
            resultType.setMessage("删除失败！");
            e.printStackTrace();
        }

        return resultType;
    }
}